import {RootState,DataItem} from "../../utils/index"
import {Toast} from "react-vant"
const initialState:RootState = {
    data:[],
    children_data: JSON.parse(window.localStorage.getItem("children_data") as string)|| [],
    details_data: JSON.parse(window.localStorage.getItem("details_data") as string)||[],
    shopcar_data: JSON.parse(window.localStorage.getItem("shopcar_data") as string)||[],
    all_show_check:false,
    all_price:0
}

const reducer = (state = initialState, { type ,  payload }:any) => {
    let newState = JSON.parse(JSON.stringify(state))
    const my_localStorage = (name:string,data:DataItem[]) =>{
        window.localStorage.setItem(name,JSON.stringify(data));
    }

    const Calculate_price = () =>{
        let num = 0
        newState.shopcar_data.forEach((item:DataItem)=>{
            if (item.checked) {
                num += (item.price as number) * (item.num as number)
            }
        })
        newState.all_price = num * 100
    }
    switch (type) {

        case "GET_LIST":
            newState.data = payload
            newState.children_data = payload[0].children
            my_localStorage("data",newState.data)
            my_localStorage("children_data",newState.children_data)
            return newState
        case "CHECK_CHILDREN_DATA":
            newState.children_data = newState.data[payload].children
            return newState
            
        case "ADD_LIST":
            newState.children_data = [...newState.children_data,...payload]
            return newState

        case "GET_DETAILS_DATA":
            newState.details_data = newState.children_data.filter((item:DataItem) =>item.id === payload)
            my_localStorage("details_data", newState.details_data)
            return newState

        case "SHOW_LIKE":
            newState.details_data[0].like = !newState.details_data[0].like
            return newState 

        case "ADD_SHOPCAR_DATA":
            const flag = newState.shopcar_data.some((item:DataItem)=> item.id === payload.id)
            if (!flag) {
                newState.shopcar_data.push(payload)
                Toast.success("添加成功")
            }else{
                newState.shopcar_data.forEach((item:DataItem,index:number)=>{
                    if (item.id === payload.id) {
                        (item.num as number) ++
                        Toast.success("num ++")
                    }
                })
            }
            Calculate_price()
            my_localStorage("shopcar_data", newState.shopcar_data)
            
            return newState 

        case "DELETE_SHOPCAR_DATA":
            newState.shopcar_data.forEach((item:DataItem, index:number) => {
                if (item.id === payload.id) {
                    newState.shopcar_data.splice(index, 1)
                    Toast.success("删除成功")
                }
            })
            Calculate_price()
            my_localStorage("shopcar_data", newState.shopcar_data)
            return newState 
        
        case "ADD_SHOPCAR_COUNT":
            newState.shopcar_data.forEach((item:DataItem, index:number) => {
                if (item.id === payload.id) {
                   (item.num as number) ++
                }
            })
            Calculate_price()
            my_localStorage("shopcar_data", newState.shopcar_data)
            return newState 

        case "DELETE_SHOPCAR_COUNT":
            newState.shopcar_data.forEach((item:DataItem, index:number) => {
                if (item.id === payload.id) {
                    (item.num as number) --
                  
                }
            })
            Calculate_price()
            my_localStorage("shopcar_data", newState.shopcar_data)
            return newState 

        case "SHOW_SHOPIFY_DATA":
           
            newState.shopcar_data.forEach((item:DataItem, index:number) => {
                if (item.id === payload.id) {
                    item.checked = !item.checked 
                }
            })
            newState.all_show_check = newState.shopcar_data.every((item:DataItem) => {
                return item.checked
            })
            Calculate_price()
            my_localStorage("shopcar_data", newState.shopcar_data)
            return newState 

        case "ALL_CHECK_SHOPIFY":
            newState.all_show_check = payload
            newState.shopcar_data.forEach((item:DataItem, index:number) => {
                item.checked = payload
            })
            Calculate_price()
            my_localStorage("shopcar_data", newState.shopcar_data)
            return newState 
            
            
        default:
            return newState
    }
}

export default reducer